﻿@model WXY.Course.Model.UserInfo

<div>
    <div class="m_rtop">
        <h3>
            <label>
                个人信息
            </label>
            <span>></span>个人详细
        </h3>
    </div>
    <div class="m_box">
        <div class="c_finfo">
            @using (Html.BeginForm("UserEdit", "Account", FormMethod.Post))
            {
                @Html.ValidationSummary()
                <p>
                    <label>
                        名称：
                    </label>
                    @Html.TextBoxFor(model => model.Name)
                    @Html.ValidationMessageFor(model => model.Name)
                </p>
                <p>
                    <label>
                        初始密码：
                    </label>
                    @Html.PasswordFor(model => model.Password)
                    @Html.ValidationMessageFor(model => model.RealName)
                </p>
                <p>
                    <label>
                        真实姓名：
                    </label>
                    @Html.TextBoxFor(model => model.RealName)
                    @Html.ValidationMessageFor(model => model.RealName)
                </p>
                @*<p>
                    <label>
                        用户级别：
                    </label>
                    @Html.EnumDropDownListFor(model => model.Level, typeof(WXY.Course.Model.UserLevel), "全部")
                    @Html.ValidationMessageFor(model => model.Level)
                </p>*@
                <p class="m_btn">
                    @Html.HiddenFor(m => m.Code)
                    <input type="submit" id="btnSubmit" class="btn_bg1" value="添加">
                    <input type="button" id="btnReturn" onclick="history.go(-1)" class="btn_bg1" value="返回">
                </p>
            }
        </div>
    </div>
</div>
<script type="text/javascript">
    $(function () {
        var generateData = function () {
            return {
                name: $("#name").val(),
                password: $("#password").val(),
                realName: $("#realName").val(),
                level: $("#level").val()
            };
        }
        var normalDialog = function (msg, fn) {
            art.dialog({
                drag: true,
                lock: true,
                width: 310,
                height: 50,
                title: '提示',
                content: msg,
                ok: function () {
                    if (fn)
                        fn();
                    else
                        this.close();
                }
            });
        };
        var valid = function (data) {
            var errorMessage = '';
            var success = true;
            if (data.name.length == 0) {
                errorMessage += '*用户名不能为空!<br/>';
                success = false;
            }
            if (data.password.length == 0) {
                errorMessage += '*密码不能为空!<br/>';
                success = false;
            }
            else if (!/^([A-Za-z]|[1-9]){6,14}$/.test(data.password)) {
                errorMessage += '*密码格式不正确,应为6-14位字母或数字组合!<br/>';
                success = false;
            }
            if (data.realName.length == 0) {
                errorMessage += '*真实姓名不能为空!<br/>';
                success = false;
            }
            if (!success)
                normalDialog(errorMessage);
            return success;
        };

        $("#btnUpdate").on("click", function () {
            var postData = generateData();
            if (valid(postData)) {
                $.ajax({
                    type: "post",
                    url: "@Url.Action("UserEdit", "Account")",
                    data: postData,
                    success: function (data) {
                        if (data.IsSuccess) {
                            normalDialog("提交成功", function () {
                                location.href = "@Url.Action("UserDetail")";
                            });
                            return;
                        }
                        normalDialog(data.Message);
                    }
                })
            }
        })
    });
</script>
